<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-plus" @click="add">新增类型</el-button>
    </div>
    <div style="font-size: 50px; color: red;">
      <el-tree :data="list" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
    </div>
    <el-dialog :title="title" :visible.sync="show">
      <el-form>
        <el-form-item label="类型名称" label-width="70px">
          <el-input v-model="item.name" />
        </el-form-item>
        <el-form-item label="类型上级" label-width="70px">
          <el-select v-model="item.pid" placeholder="变动类型" clearable style="width: 150px" class="filter-item">
            <el-option v-for="(item, index) in list" :key="index" :label="item.name" :value="item.id" />
            <el-option :value='0' label='顶级分类' />
          </el-select>
          <!-- <el-input v-model="item.pid" /> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="del()">删 除</el-button>
        <el-button type="primary" @click="handle()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    libraryCateEdit,
    libraryCateList,
    libraryCateDel,
  } from '@/api/library';

  import waves from '@/directive/waves';
  import {
    parseTime
  } from '@/utils';
  import Pagination from '@/components/Pagination';
  import permission from '@/directive/permission/index.js';
  import {
    dateHandle
  } from '@/api/dateHandle';
  import datePicker from '@/components/Upload/datePicker';
  export default {
    components: {
      Pagination,
      datePicker
    },
    directives: {
      waves,
      permission,

    },
    data() {
      return {
        list: [],
        defaultProps: {
          children: 'child',
          label: 'name'
        },
        title: '新增类型',
        show: false,
        item: {}
      };
    },
    created() {
      this.getList()

    },
    methods: {
      del() {
        this.$confirm('确认删除该分类?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log(111);
          libraryCateDel({
            id: this.item.id
          }).then(res => {
            console.log(res);
            if (res.code == 20000) {
              this.$message({
                type: 'success',
                message: res.msg
              });
              this.show = false;
              this.getList()
            }
          })
        }).catch(() => {})
      },
      handle() {
        libraryCateEdit(this.item).then(res => {
          if (res.code == 20000) {
            this.$message({
              type: 'success',
              message: res.msg
            });
            this.show = false;
            this.getList()
          }
        })
      },
      add() {
        this.show = true
        this.title = '新增题目类型'
        this.item = {}
      },
      edit(data) {
        this.item = data
        this.show = true
        this.title = '编辑题目类型'
      },
      _date(t) {
        return dateHandle('Y-m-d H:i:s', t);
      },
      handleNodeClick(data) {
        this.edit(data)
        console.log(data)
      },
      getList() {
        this.listLoading = true;
        libraryCateList(this.listQuery).then(res => {
          this.list = res.data;
          console.log(this.list);
          setTimeout(() => {
            this.listLoading = false;
          }, 0.3 * 1000);
        });
      },

    }
  };
</script>
